﻿using System;
using GCTrackableRace.Interfaces;
using System.IO;

namespace GcTrackableRace.Persistence
{
	public class MissingCacheLog: IMissingCacheLog
	{
		public MissingCacheLog (IProgramConfigurationSettings programConfigurationSettings)
		{
			ProgramConfigurationSettings = programConfigurationSettings;
		}

		private IProgramConfigurationSettings ProgramConfigurationSettings { get; set; }

		public void Add(string gcCode)
		{
			var fileNameAndLocation = ProgramConfigurationSettings.GetOutputFileNameAndLocation(FileType.MissingInfoLogFile);

			if (!File.Exists(fileNameAndLocation))
			{
				var fileContentsHeader = String.Format(
					"The following geocaches were not found in the '{0}' database.\r\n" +
					"Please add in these geocaches so the program can get the information\r\n",
					ProgramConfigurationSettings.GeocacheDatabaseNameAndLocation);

				File.WriteAllText(fileNameAndLocation, fileContentsHeader);
			}

			string fileContents = File.ReadAllText(fileNameAndLocation);

			if (!fileContents.Contains(gcCode))
			{
				fileContents = String.Format("{0}{1};", fileContents, gcCode);
				File.WriteAllText(fileNameAndLocation, fileContents);
			}
		}
	}
}
